migrate: convert strings.ipynb from pandas to polars - #12
Conversation
brentomagic
left a comment
There was a problem hiding this comment.
Thanks for the strings migration — prerequisites, method table, to_titlecase / to_lowercase, extract, and the split → list.to_struct().unnest() pattern look good, and CI is green. A few fixes before merge:
Must-fix
-
Lost “last token after split” example
Original taughtsplit().str.get(-1)(last name). That was replaced with:df.with_columns(pl.col("names").str.slice(0, 5))
which teaches something different. Please restore the last-token idea with polars list access, e.g.:
df.with_columns(pl.col("names").str.split(" ").list.get(-1).alias("last"))
Keep a
slice()example if you want, but don’t drop the split→get teaching point. -
get_dummies→ column split needs clearer prose
Originalstr.get_dummies(";")produced one-hot indicator columns. The new code:.str.split("; ").list.to_struct(...).unnest("tags")
splits into fixed
country/subjectcolumns instead. That’s fine for this dataset, but the text should say so explicitly (and not imply it’s the same as dummy encoding). Optionally show a polars-native dummy approach later (explode+pivot/to_dummies) if you want to keep that skill.
Medium / nits
- Grammar: “we can use the `str.split()` to split” → “use `str.split()` …”
- Kernelspec
display_name: ".venv"→ genericPython 3 (ipykernel)(same note as other PRs). - Commit author metadata is still
Your Name— please set git user.name/email for future commits.
Happy to approve after the last-token example (and clearer dummies→split prose) are sorted.
Show both str.slice and split().list.get(-1), and clarify fixed-field splitting versus dummy encoding. Co-authored-by: Cursor <cursoragent@cursor.com>
Summary
Converts the
strings.ipynbchapter from pandas to polars, covering string manipulation and text processing.Changes
Code
pd.Series()→pl.Series().str.lower()→.str.to_lowercase().str.upper()→.str.to_uppercase().str.title()→.str.to_titlecase().str.extract()→.str.extract().str.split(expand=True)→.list.to_struct().unnest()Text
Testing
Files Modified
strings.ipynb